package com.example.hbutliving.util;

import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.ThreadContext;

/**
 * 日志工具类
 * @author lizijian
 */
public class LogUtil {

    private static final String TRACE_ID= "traceId";

    private static final String TIME="time";


    public static String getTaskId() {
        if (ThreadContext.get(TRACE_ID) == null) {
            ThreadContext.put(TRACE_ID,UUID.randomUUID().toString().replace("-",""));
        }
        return ThreadContext.get(TRACE_ID);
    }

    public static Long getTime(){
        Long endTime = System.nanoTime();
        Long beginTime = Long.valueOf(ThreadContext.get(TIME));
        return TimeUnit.NANOSECONDS.toMillis(endTime-beginTime);
    }

    public static void setTaskId(String taskId){
        ThreadContext.put(TRACE_ID,taskId);
    }

    public static void setTime(){
        ThreadContext.put(TIME,String.valueOf(System.nanoTime()));
    }

    public static void removeTaskId() {
        ThreadContext.remove(TRACE_ID);
    }

    public static void removeAll(){
        ThreadContext.clearAll();
    }

}
